package org.xtframe.drools.business;

import org.drools.runtime.StatefulKnowledgeSession;
import org.xtframe.drools.vo.BusinessResult;
import org.xtframe.drools.vo.SqlMessage;
import org.xtframe.util.ContextUtil;
import org.xtframe.util.SpringUtil;

public class BusinessExecutor {
	// 判定SQL执行权限
	public static BusinessResult verifySQLExecutivePower(String sqlID){
		BusinessResult result = new BusinessResult();
		
		SqlMessage sqlMessage = new SqlMessage();
		sqlMessage.setSqlID(sqlID);
		sqlMessage.setUserData(ContextUtil.getCurrentUserData());
		sqlMessage.setServerData(ContextUtil.getServerData());
		
		StatefulKnowledgeSession kstateless = (StatefulKnowledgeSession) SpringUtil.getBean("sqlKsession");
        kstateless.insert(sqlMessage);
        kstateless.fireAllRules();
        kstateless.dispose();
        
        if(sqlMessage.getStatus() == SqlMessage.SUCCESS) result.setResult(BusinessResult.SUCCESS);
        else if(sqlMessage.getStatus() == SqlMessage.LOGIN_AGAIN) result.setResult(BusinessResult.LOGIN_AGAIN);
        else result.setResult(BusinessResult.FAILURE);
        result.setMessage(sqlMessage.getMessage());
        
		return result;
	}
}
